package handlers;

import influence_radii.InfluenceRadius;

import java.util.ArrayList;
import java.util.List;

import map.Direction;
import map.GameMap;
import map.GameWorld;
import map.Position;
import effects.CombatEffect;
import entity.Entity;

public class CEMvtHandler {
	private CombatEffect ce = null;
	private Position startPos = null;
	private Direction dir = null;
	private int currentTier;
	private List<Position> previousLocations;
	private InfluenceRadius ir; 
	
	public CEMvtHandler(CombatEffect ce, Entity e) {
		this.ce = ce;
		this.startPos = new Position();
		startPos.add( e.getPosition() );
		this.dir = e.getDirection();
		this.currentTier = ce.getMinRange();
		previousLocations = new ArrayList<Position>();
		ir = ce.getInfluenceRadius();
	}
	
	/**
	 * Removes any CombatEffects in the previous tier locations.
	 * Spawns the next tier of propogations.
	 * Interacts the combat effect with those entities that collide.
	 */
	public void propagate() {
		GameMap map = GameWorld.getInstance().getCurrentMap();
		
		ir.setDirection( dir );
		previousLocations = ir.getRelAppsPts( currentTier++ );
		for( Position p : previousLocations ) {
			p.add( startPos );
			
			map.addCombatEffect( ce, p );
			
			if( map.hasEntity( p ) ) {
				ce.accept( map.getEntity( p ).getEffectVisitor() );
			}
			
			ce.incrementDistTraveled();
		}
	}
	
	public void clean() {
		GameMap map = GameWorld.getInstance().getCurrentMap();
		for( Position p : previousLocations ) {
			if( map.hasCombatEffect( p ) && map.getCombatEffect( p ) == ce ) {
				map.removeCombatEffect( p );
			}
		}
	}
	
}
